const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const { HtmlExtPlugin } = require("falcon-webpack");

const { resources } = require("./config/config.json");

module.exports = {
  entry: ["babel-polyfill", "./src/apps/dev.tsx"],
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: "xSite-[name].js",
    chunkFilename: "xSite-[name].js"
  },
  externals: {
    QMDecorator: "window.QMDecorator",
    "babel-polyfill": "window._babelPolyfill"
  },
  devServer: {
    port: 8080,
    disableHostCheck: true,
    host: "127.0.0.1"
  },
  resolve: {
    modules: [
      ".",
      "node_modules",
      path.resolve(__dirname, "./src/web_modules")
    ],
    extensions: [".tsx", ".ts", ".web.js", ".js", ".json"]
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loaders: ["babel-loader?cacheDirectory=true", "ts-loader"]
      },
      { test: /\.js/, loader: "babel-loader?cacheDirectory" },
      {
        test: /\.less$/,
        loader: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: ["css-loader", "less-loader"]
        })
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      },
      { test: /\.(png|gif)$/, loader: "file-loader" }
    ]
  },
  plugins: [
    new webpack.BannerPlugin("This file is created by qm"),
    new ExtractTextPlugin({
      filename: "[name].css",
      disable: false,
      allChunks: true
    }),
    new webpack.DefinePlugin({
      XSite_ENV: JSON.stringify("test"),
      __DEBUG__: false,
      __DEV__: false
    }),
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: "./index.ejs"
    }),
    new HtmlExtPlugin({
      env: "x", //.dev | 0,1,2,3,4,5 | null
      context: __dirname,
      filename: ["./config/config.json", "./config/bundles.json"]
    }),
    new webpack.DllReferencePlugin({
      name: "genesis",
      context: __dirname,
      manifest: require("./config/genesis.dll.json")
    })
  ]
};
